<?php
namespace Home\Controller;

use Org\Util\Sms;
use Org\Util\smtp;
class LoginController extends HomeBaseController
{
   
    public function index()
    {
        $this->display('login/index');
    }
    
    
    /**
     * 密码登录
     * @param unknown $uid  $paswd
     */
    public function userLoginByPaswd()
    {
        $uid=I('uid','');
        $paswd=I('paswd','');
        if(empty($uid) || empty($paswd)) {
            $this->ajaxReturn(array('status'=>'2','msg' => '登录用户和密码不能为空！'));//0错误，1成功
            exit;
        }
        $user= D('User')->getUserByPasswd($uid,$paswd);
        if(empty($user)){
            $this->ajaxReturn(array('status'=>'2','msg' => '用户名或密码错误，请重新登录！'));//0错误，1成功
            exit;
        }
        D('User')->updateLoginDate($user[0]['id'],null);
        session('uid', $user[0]['id']);
        if(!empty($user[0]['mobile'])){
            session('username', $user[0]['mobile']);
        }
        else if(!empty($user['email'])){
            session('username', $user[0]['email']);
        }
        $this->ajaxReturn(array('status'=>'1','redirect' => U("Index/index"),'msg'=>'登录成功,请稍候......'));//0错误，1成功
    }
    
    
    /**
     * 发送短信验证码(登录)
     */
    public function sendLoginSms(){
        $mobile=I('mobile','');
        if (!is_numeric($mobile)) {
            $this->ajaxReturn(array('code'=>'error', 'info' => '手机号码为空！'));
            exit;//0错误，1成功
        } 
        if(!preg_match('#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#', $mobile)) {
            $this->ajaxReturn(array('code'=>'error','info' => '手机号码格式错误！'));//0错误，1成功
            exit;
        }
       $user = D("User")->getUserByMobile($mobile);
       if($user==null){
            $this->ajaxReturn(array('code'=>'error','info' => '此手机号码暂未注册，请先去注册！'));//0错误，1成功
            exit;
        } 
        $code = mt_rand(100000, 999999);
        $text = '您的验证码是：' . $code . '。此验证码1分钟内有效，请不要把验证码泄露给其他人。如非本人操作，可不用理会！';
        $send_time = time();
        $expire_time  = time() + 60;
        $sendObj=M('Sendkey')->find(1);
        $return = Sms::sendSms(array( 'content' => $text, 'mobile' => $mobile,
            'expire_time'=>$expire_time,'extra'=>$code,'sign'=>$sendObj['sign'],'todomain'=>$sendObj['todomain'],'key'=>$sendObj['key']));
        
        if($return=='0' || $return==0) {
            session(array('name'=>$mobile.'code','expire'=>70));
            session($mobile.'code',$code);
            $this->ajaxReturn(array('code'=>'success','info' => '获取验证码成功，请及时查看！'));//0错误，1成功
        } else {
            $this->ajaxReturn(array('code'=>'error','info' => '验证码发送失败！'));//0错误，1成功
        }
        $this->ajaxReturn(array('code'=>'success','info' => '获取验证码成功，请及时查看！')); 
    }
    
    
    /**
     * 短信验证码登录
     * @param unknown $mobile $code
     */
    public function userLoginByMobile()
    {
        $mobile=I('mobile','');
        $code=I('code','');
        if(empty($mobile) || empty($code)) {
            $this->ajaxReturn(array('status'=>'2','msg' => '手机号码或验证码不能为空！'));//0错误，1成功
            exit;
        }
        $sessioncode = session($mobile.'code');
        $sessioncode=strval($sessioncode);
        if(empty($sessioncode) || $code!=$sessioncode) {
            $this->ajaxReturn(array('status'=>'2','msg' => '验证码错误或已过期！'));//0错误，1成功
            session($mobile.'code',null);
            exit;
        }
        session($mobile.'code',null);
        $user = D("User")->getUserByMobile($mobile);
        if(empty($user)) {
            $this->ajaxReturn(array('status'=>'2','msg' => '用户不存在,请先注册！'));//0错误，1成功
            exit;
        }
        D('User')->updateLoginDate($user[0]['id'],null);
        session('uid', $user[0]['id']);
        session('username', $mobile);
        $this->ajaxReturn(array('status'=>'1','redirect' => U("Index/index"),'msg'=>'登录成功,请稍候......'));//0错误，1成功
    }
    
    public function logout(){
        session('uid',null);
        session('username',null);
        redirect(U('Index/index'));
    }
    
    public function forget(){
        $this->display('login/forget');
    }
    
    
    /**
     * 短信找回密码
     */
    public function findPaswdByMobile()
    {
        $mobile=I('mobile','');
        $code=I('code','');
        $passwd=I('passwd','');
        $repasswd=I('repasswd','');
        if(empty($mobile) || empty($code)) {
            $this->ajaxReturn(array('status'=>'2','msg' => '手机号码或验证码不能为空！'));//0错误，1成功
            exit;
        }
        if(empty($passwd) || empty($repasswd)) {
            $this->ajaxReturn(array('status'=>'2','msg' => '密码不能为空！'));//0错误，1成功
            exit;
        }
        if($passwd!=$repasswd) {
            $this->ajaxReturn(array('status'=>'2','msg' => '重复密码和输入密码不同！'));//0错误，1成功
            exit;
        }
        $sessioncode = session($mobile.'code');
        $sessioncode=strval($sessioncode);
        if(empty($sessioncode) || $code!=$sessioncode) {
            $this->ajaxReturn(array('status'=>'2','msg' => '验证码错误或已过期！'));//0错误，1成功
            session($mobile.'code',null);
            exit;
        }
        session($mobile.'code',null);
        $user = D("User")->getUserByMobile($mobile);
        if(empty($user)) {
            $this->ajaxReturn(array('status'=>'2','msg' => '用户不存在,请先注册！'));//0错误，1成功
            exit;
        }
        D("User")->getUserByMobile($user[0]['id'],$passwd);
        $this->ajaxReturn(array('status'=>'1','redirect' => U("Login/index"),'msg'=>'密码重置成功，请登录......'));//0错误，1成功
    }
    
    /**
     * 发送注册email
     * */
    public function sendfindPaswdEmail(){
        $email=I('email','');
        if(!preg_match('/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/', $email)) {
            $this->ajaxReturn(array('code'=>'error','info' => '邮箱地址格式错误！'));//0错误，1成功
            exit;
        }
        $user = D('User')->getUserByEmail($email);
        if($user==null){
            $this->ajaxReturn(array('code'=>'error','info' => '此邮箱暂未注册！'));//0错误，1成功
            exit;
        }
        $code = mt_rand(100000, 999999);
        $mail_title = '欢迎您，您的验证码';
        $mail_body = <<<Eof
尊敬的 $email 用户您好，您的验证码是 ：$code 。请在1分钟之内使用！<br/>
http://www.chengjie.com <br/>
联系电话：+86 0513-886-99963<br/>
客服邮箱: admin@chengjie.com
Eof;
        smtp::sendSmsContext($mail_body, $mail_title,$email);
        session(array('name'=>'registerCode','expire'=>70));
        session('updatepswdCode',$code);
        $this->ajaxReturn(array('code'=>'success','info' => '邮件发送成功，请及时查看！'));//0错误，1成功
    }
    
    
    /**
     * email找回密码
     */
    public function findPaswdByEmail()
    {
        $email=I('email','');
        $code=I('emailcode','');
        $passwd=I('passwd','');
        $repasswd=I('repasswd','');
        if(empty($email) || empty($code)) {
            $this->ajaxReturn(array('status'=>'2','msg' => 'E-mail或验证码不能为空！'));//0错误，1成功
            exit;
        }
        if(empty($passwd) || empty($repasswd)) {
            $this->ajaxReturn(array('status'=>'2','msg' => '密码不能为空！'));//0错误，1成功
            exit;
        }
        if($passwd!=$repasswd) {
            $this->ajaxReturn(array('status'=>'2','msg' => '重复密码和输入密码不同！'));//0错误，1成功
            exit;
        }
        $sessioncode = session('updatepswdCode');
        $sessioncode=strval($sessioncode);
        if(empty($sessioncode) || $code!=$sessioncode) {
            $this->ajaxReturn(array('status'=>'2','msg' => '验证码错误或已过期！'));//0错误，1成功
            session('updatepswdCode',null);
            exit;
        }
        session('updatepswdCode',null);
        $user = D("User")->getUserByEmail($email);
        if(empty($user)) {
            $this->ajaxReturn(array('status'=>'2','msg' => '用户不存在,请先注册！'));//0错误，1成功
            exit;
        }
        D("User")->getUserByMobile($user[0]['id'],$passwd);
        $this->ajaxReturn(array('status'=>'1','redirect' => U("Login/index"),'msg'=>'密码重置成功，请登录......'));//0错误，1成功
    }
    
    
    public function echart(){
        $this->display('login/echart');
    }
    
}